//
//  SearchResultVC.m
//  ProjectsOne
//
//  Created by iOS开发 on 2018/7/11.
//  Copyright © 2018年 iOS开发. All rights reserved.
//

#import "SearchResultVC.h"
#import "SearchView.h"
#import "SearchResultHeadView.h"
#import "SearchResultFootView.h"
#import "SearchResultCell.h"
#import "SearchResultModel.h"
#import "NoDataManager.h"

@interface SearchResultVC ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) SearchView *searchView;
@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, copy) NSString *searchText;

@property (nonatomic, strong) NSMutableArray *classArr;

@end

@implementation SearchResultVC

- (void)dealloc{
    NSLog(@"%@释放",NSStringFromClass([self class]));
}

- (instancetype)initWithSearchText:(NSString *)text {
    self = [super init];
    if (self) {
        _searchText = text;
        [self requestData];
    }
    return self;
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.rowHeight = 95;
        _tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
        
        [_tableView registerClass:[SearchResultCell class] forCellReuseIdentifier:@"searchResultCell"];
        
    }
    return _tableView;
}

- (SearchView *)searchView {
    if (!_searchView) {
        _searchView = [[SearchView alloc] initWithFrame:CGRectMake(10, 100, WIDTH, 44)];
        _searchView.userInteractionEnabled = NO;
        self.navigationItem.titleView = _searchView;
    }
    return _searchView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setNav];
    [self createView];
}

- (void)createView {
    self.searchView.searchFiled.text = self.searchText;
    [self.view addSubview:self.tableView];
    
    // 判断是否有数据
    __block BOOL isHaveData = NO;
    for (SearchResultClassModel *classModel in self.classArr) {
        if (classModel.resultArr.count != 0) {
            isHaveData = YES;
        }
    }
    
    if (!isHaveData) {
        [[NoDataManager sharedInstace] showNodataViewToAddSubView:self.tableView];
    }
}

- (void)setNav {
    self.view.backgroundColor = [UIColor whiteColor];
    
    // 返回
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton setImage:[UIImage imageNamed:@"top_return_arrow_p"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(popAction) forControlEvents:UIControlEventTouchUpInside];
    backButton.frame = CGRectMake(0, 0, 30, 40);
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    // 右滑
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
}



#pragma mark - data
- (void)requestData {
    _classArr = [NSMutableArray array];
    SearchResultClassModel *classOne = [[SearchResultClassModel alloc] init];
    [classOne setValue:@"分类一" forKey:@"classTitle"];
    [_classArr addObject:classOne];
    if (!classOne.resultArr.count) {
        [_classArr removeObject:classOne];
    }
    
    SearchResultClassModel *classTwo = [[SearchResultClassModel alloc] init];
    [classTwo setValue:@"分类二" forKey:@"classTitle"];
    [_classArr addObject:classTwo];
    if (!classTwo.resultArr.count) {
        [_classArr removeObject:classTwo];
    }
}



#pragma mark - tableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.classArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    SearchResultClassModel *classModel = self.classArr[section];
    return classModel.isMore?3:classModel.resultArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SearchResultCell *cell = [tableView dequeueReusableCellWithIdentifier:@"searchResultCell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    SearchResultClassModel *classModel = self.classArr[indexPath.section];
    cell.model = classModel.resultArr[indexPath.row];
    
    if ([classModel.classTitle isEqualToString:@"分类一"]) {
        cell.cellType = UITableViewCellPicture;
    } else {
        cell.cellType = UITableViewCellHead;
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    SearchResultClassModel *classModel = self.classArr[section];
    return classModel.resultArr.count != 0?48:0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    SearchResultClassModel *classModel = self.classArr[section];
    if (classModel.isMore) {
        return 48;
    } else if (classModel.resultArr.count != 0) {
        return 8;
    } else {
        return 0;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    SearchResultHeadView *headView = [[SearchResultHeadView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 48)];
    SearchResultClassModel *classModel = self.classArr[section];
    headView.classLabel.text = classModel.classTitle;
    headView.backgroundColor = [UIColor whiteColor];
    return headView;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    SearchResultFootView *foot = [[SearchResultFootView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 48)];
    foot.backgroundColor = [UIColor colorWithHexString:@"f0f0f0"];
    SearchResultClassModel *classModel = self.classArr[section];
    if (classModel.isMore) {
        foot.bgView.hidden = NO;
    } else {
        foot.bgView.hidden = YES;
    }
    return foot;
}

// 返回
- (void)popAction {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    if (self.movingFromParentViewController) {
        if (self.block) {
            self.block();
        }
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
